package com.asj.house.service.impl;

import com.asj.house.entity.HouseRoom;
import com.asj.house.entity.HouseRoomRecommend;
import com.asj.house.mapper.HouseRoomRecommendMapper;
import com.asj.house.returns.RespCode;
import com.asj.house.returns.RespEntity;
import com.asj.house.service.HouseRoomRecommendService;
import com.asj.house.service.HouseRoomService;
import com.asj.house.utils.ConverterUtil;
import com.asj.house.vo.result.RoomRecommendResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 房屋推荐表 服务实现类
 * </p>
 *
 * @author asj
 * @since 2021-03-05
 */
@Service
public class HouseRoomRecommendServiceImpl extends ServiceImpl<HouseRoomRecommendMapper, HouseRoomRecommend> implements HouseRoomRecommendService {

    @Autowired
    private HouseRoomService houseRoomService;

    @Override
    public RespEntity<List<RoomRecommendResult>> roomRecommendList() {
        QueryWrapper<HouseRoomRecommend> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HouseRoomRecommend::getDeleted, 0);

        List<HouseRoomRecommend> houseRoomRecommendList = baseMapper.selectList(queryWrapper);

        if (Objects.isNull(houseRoomRecommendList) || houseRoomRecommendList.size() == 0) {
            return new RespEntity<>(RespCode.SUCCESS);
        }

        List<Integer> roomIdList = houseRoomRecommendList.stream()
                .map(HouseRoomRecommend::getRoomId)
                .collect(Collectors.toList());

        List<HouseRoom> houseRoomList = houseRoomService.listByIds(roomIdList);

        List<RoomRecommendResult> roomRecommendResultList = ConverterUtil.copyList(houseRoomList, RoomRecommendResult.class);

        //数据组装
        roomRecommendResultList = dataPackage(roomRecommendResultList, houseRoomRecommendList);

        return new RespEntity<>(RespCode.SUCCESS, roomRecommendResultList);
    }

    private List<RoomRecommendResult> dataPackage(List<RoomRecommendResult> roomRecommendResultList, List<HouseRoomRecommend> houseRoomRecommendList) {
        for (int i = 0; i < roomRecommendResultList.size(); i++) {
            roomRecommendResultList.get(i).setSeconds(houseRoomRecommendList.get(i).getSeconds());
        }
        return roomRecommendResultList;
    }
}
